using Microsoft.Extensions.Options;
using SendGrid;
using SendGrid.Helpers.Mail;

namespace BuildingBlocks.Infrastructure.Email;

public class SendGridEmailSender : IEmailSender
{
    private readonly EmailSettings _settings;

    public SendGridEmailSender(IOptions<EmailSettings> settings)
    {
        _settings = settings.Value;
    }

    public async Task SendEmailAsync(string to, string subject, string body, bool isHtml = true)
    {
        var client = new SendGridClient(_settings.SendGridApiKey);
        var from = new EmailAddress(_settings.SenderEmail, _settings.SenderName);
        var toEmail = new EmailAddress(to);
        var msg = MailHelper.CreateSingleEmail(from, toEmail, subject,
            isHtml ? null : body,
            isHtml ? body : null);

        await client.SendEmailAsync(msg);
    }
}